/**
 * Copyright (c) 2016-2023, Michael Yang 杨福海 (fuhai999@gmail.com).
 * <p>
 * Licensed under the GNU Lesser General Public License (LGPL) ,Version 3.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.gnu.org/licenses/lgpl-3.0.txt
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.jpress.web.directive;

import com.jfinal.aop.Inject;
import com.jfinal.template.Env;
import com.jfinal.template.io.Writer;
import com.jfinal.template.stat.Scope;
import io.jboot.utils.DateUtil;
import io.jboot.web.directive.annotation.JFinalDirective;
import io.jboot.web.directive.base.JbootDirectiveBase;
import io.jpress.SiteContext;
import io.jpress.service.UtmService;

/**
 * @author Michael Yang 杨福海 （fuhai999@gmail.com）
 * @version V1.0
 * @Package io.jpress.core.directives
 */
@JFinalDirective("sitePV")
public class SitePVDirective extends JbootDirectiveBase {

    @Inject
    private UtmService utmService;

    @Override
    public void onRender(Env env, Scope scope, Writer writer) {

        Long siteId = getParaToLong("siteId", scope, SiteContext.getSiteId());

        boolean queryTodayPV = getParaToBool("queryTodayPV", scope, false);
        boolean queryWeekPV = getParaToBool("queryWeekPV", scope, false);
        boolean queryMonthPV = getParaToBool("queryMonthPV", scope, false);
        boolean queryQuarterPV = getParaToBool("queryQuarterPV", scope, false);
        boolean queryYearPV = getParaToBool("queryYearPV", scope, false);
        boolean queryTotalPV = getParaToBool("queryTotalPV", scope, false);

        if (queryTodayPV) {
            Long todayPV = utmService.queryPvBySiteId(siteId, DateUtil.getStartOfToday(), null);
            scope.setLocal("todayPV", todayPV);
        }

        if (queryWeekPV) {
            Long weekPV = utmService.queryPvBySiteId(siteId, DateUtil.getStartOfThisWeek(), null);
            scope.setLocal("weekPV", weekPV);
        }

        if (queryMonthPV) {
            Long monthPV = utmService.queryPvBySiteId(siteId, DateUtil.getStartOfThisMonth(), null);
            scope.setLocal("monthPV", monthPV);
        }

        if (queryQuarterPV) {
            Long quarterPV = utmService.queryPvBySiteId(siteId, DateUtil.getStartOfThisQuarter(), null);
            scope.setLocal("quarterPV", quarterPV);
        }

        if (queryYearPV) {
            Long yearPV = utmService.queryPvBySiteId(siteId, DateUtil.getStartOfThisYear(), null);
            scope.setLocal("yearPV", yearPV);
        }

        if (queryTotalPV) {
            Long totalPV = utmService.queryPvBySiteId(siteId, null, null);
            scope.setLocal("totalPV", totalPV);
        }

        renderBody(env,scope,writer);
    }

    @Override
    public boolean hasEnd() {
        return true;
    }
}

